package net.hn.hnms.biz.safety.location.service;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.api.plan.RemoteShippedService;
import net.hn.hnms.biz.safety.location.domain.DutyLeadEntity;
import net.hn.hnms.biz.safety.location.repository.DutyLeadEntityRepository;
import net.hn.hnms.sys.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: PanSX
 * Date: 2023/5/9 16:32
 */
@Slf4j
@Service
public class JobService {
    @Autowired
    private LocationService locationService;
    @Autowired
    private DutyLeadEntityRepository dutyLeadEntityRepository;

    @Resource
    private RemoteShippedService remoteShippedService;
    @XxlJob("dutyAndLeadHandler")
    public void dutyAndLead() throws Exception {
        String param = XxlJobHelper.getJobParam();
        //判断是否是高头窑，如果是高头窑就直接获取http文件的数据
        Map<String, String> stringStringMap = new HashMap<>();
        if ("150621011038".equals(param)) {
            //高头窑
            stringStringMap= locationService.gaotouyaoHttpRTF();
        } else {
            stringStringMap = locationService.ftpUtile(param);
        }
        DutyLeadEntity entity=new DutyLeadEntity();
        entity.setDuty(stringStringMap.get("duty"));
        entity.setLead(stringStringMap.get("lead"));
        entity.setMineCode(param);
        dutyLeadEntityRepository.save(entity);
    }



    @XxlJob("railroadLoadingHandler")
    public void railroadLoadingHandler() throws Exception {
        //获取铁路装车的数据
        //IpYear  {"records":"-1","pageNo":0,"pageSize":20,"message":"success","content":[{"yeartime":"2024.0","realweight":"1293712.45"}],"successFlag":true}
        //IpLastMonth  {"records":"-1","pageNo":0,"pageSize":20,"message":"success","content":[{"monthtime":"2024-03","sum":"425951.00"}],"successFlag":true}
//        String IpYear = "http://10.108.90.132/open/api/1/ratilway/load/daily/year?pageNo=0&pageSize=20&masterKey=exEQhbVqMPLFAXbn1usA&appKey=zV5DRBy04ehSgOCBo4PV";
//        String IpLastMonth = "http://10.108.90.132/open/api/1/railway/load/daily/lastmonth?pageNo=0&pageSize=20&type=-1&masterKey=exEQhbVqMPLFAXbn1usA&appKey=zV5DRBy04ehSgOCBo4PV&0=12";
//
//        //获取年数据
//        RailwayLoadData<RailwayLoadData.YearData> yearDataRailwayLoadData = GroupLocationService.sendGetRequest(IpYear, null, new TypeReference<RailwayLoadData<RailwayLoadData.YearData>>() {});
//        //获取月数据
//        XxlJobHelper.log("yearDataRailwayLoadData"+yearDataRailwayLoadData);
//        RailwayLoadData<RailwayLoadData.LastMonthData> LastMonthData = GroupLocationService.sendGetRequest(IpLastMonth, null, new TypeReference<RailwayLoadData<RailwayLoadData.LastMonthData>>() {});
//
//        XxlJobHelper.log("LastMonthData"+LastMonthData);

        String yearTime = "";
        String yearRealWeight = "";
        String LastMonthTime = "";
        String LastMonthsum = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String DateTime = simpleDateFormat.format(date);
//        if (yearDataRailwayLoadData != null && yearDataRailwayLoadData.getContent()[0] != null) {
//            //获取到了年数据
//            yearTime=yearDataRailwayLoadData.getContent()[0].getYearTime();
//            yearRealWeight=yearDataRailwayLoadData.getContent()[0].getRealWeight();
//            XxlJobHelper.log("yearTime+yearRealWeight"+yearTime+"---"+yearRealWeight);
//        }
//        if (LastMonthData != null && LastMonthData.getContent()[0] != null) {
//            //获取到了月数据
//            LastMonthTime=LastMonthData.getContent()[0].getMonthTime();
//            LastMonthsum=LastMonthData.getContent()[0].getSum();
//            XxlJobHelper.log("LastMonthTime+LastMonthsum"+LastMonthTime+"---"+LastMonthsum);
//        }
//
//        XxlJobHelper.log("yearTime"+yearTime+"yearRealWeight"+yearRealWeight+"LastMonthTime"+LastMonthTime+"LastMonthsum"+LastMonthsum);
//        //保存数据
        XxlJobHelper.log("DateTime="+DateTime);
        R<Boolean> booleanR = remoteShippedService.saveRailroadload(yearTime, yearRealWeight, LastMonthTime, LastMonthsum, date, DateTime);
        if (booleanR.getData() == Boolean.TRUE) {
            XxlJobHelper.log("保存成功");
        }

    }

}
